/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ["admin", "editor"];
  return valid_map.indexOf(str.trim()) >= 0;
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg =
    /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
  return reg.test(url);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/;
  return reg.test(str);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/;
  return reg.test(str);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/;
  return reg.test(str);
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
  const reg =
    /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return reg.test(email);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === "string" || str instanceof String) {
    return true;
  }
  return false;
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === "undefined") {
    return Object.prototype.toString.call(arg) === "[object Array]";
  }
  return Array.isArray(arg);
}

/**
 **数字金额校验(请输入正数或0，且小数点后最多保留digit位)
 * @param {Number} num
 * @param {Number} digit
 * @returns {Boolean}
 */
export function validMoney(num, digit) {
  if (num == 0 || num == null) {
    return true;
  } else {
    const txt =
      "^(?!0+(?:\\.0+)?$)(?:[1-9]\\d*|0)(?:\\.\\d{1," + digit + "})?$";
    const reg = new RegExp(txt);
    // const reg = /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,4})?$/;
    return reg.test(num);
  }
}

/**
 **电话校验
 * @param {Number} phone
 * @returns {Boolean}
 */
export function validPhoe(phone) {
  const reg = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
  return reg.test(phone);
}

/**
 **手机校验
 * @param {Number} mobile
 * @returns {Boolean}
 */
export function validMobile(mobile) {
  const reg = /^[1][3,5,7,8][0-9]{9}$/;
  return reg.test(mobile);
}

/**
 **网址校验
 * @param {Number} url
 * @returns {Boolean}
 */
export function validWeburl(url) {
  const txt =
    "^((https|http|ftp|rtsp|mms)?://)" +
    "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" + //ftp的user@
    "(([0-9]{1,3}.){3}[0-9]{1,3}" + // IP形式的URL- 199.194.52.184
    "|" + // 允许IP和DOMAIN（域名）
    "([0-9a-z_!~*'()-]+.)*" + // 域名- www.
    "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]." + // 二级域名
    "[a-z]{2,6})" + // first level domain- .com or .museum
    "(:[0-9]{1,4})?" + // 端口- :80
    "((/?)|" + // a slash isn't required if there is no file name
    "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
  const reg = new RegExp(txt);
  return reg.test(url);
}

/**
 **设备编码校验-电表
 * @param {String} val
 * @returns {Boolean}
 */
export function validDeviceCode(val) {
  const reg = /^[A-Fa-z0-9]+\-(\d{1}|\d{2})$/;
  return reg.test(val);
}

/**
 **设备编码校验-水表
 * @param {String} val
 * @returns {Boolean}
 */
export function validDeviceCode2(val) {
  const reg = /^\d{12}$/;
  return reg.test(val);
}

/**
 **数字英文校验
 * @param {String} val
 * @returns {Boolean}
 */
export function validAlphanumeric(val) {
  const reg = /^[0-9a-zA-Z_]{1,}$/;
  return reg.test(val);
}

/**
 **版本号校验
 * @param {Number} val
 * @returns {Boolean}
 */
export function validVersions(val) {
  const reg = /^-?[1-9]\d{10}$/;
  return reg.test(val);
}

/**
 **电压等级校验（正整数包括0,"",null）
 * @param {Number} val
 * @returns {Boolean}
 */
export function validPositiveInteger(val) {
  // if (val == "") {
  //   return true;
  // } else if (val == null) {
  //   return true;
  // } else {
  const reg = /^([0]|[1-9][0-9]*)$/;
  return reg.test(val);
  // }
}
